package FSMDE.persistence.tests;

import static org.junit.Assert.assertEquals;

import java.util.UUID;

import javax.xml.parsers.ParserConfigurationException;

import main.java.FSMDE.models.Interface;
import main.java.FSMDE.models.InterfaceCollection;
import main.java.FSMDE.models.InterfaceGraphType;
import main.java.FSMDE.models.InterfaceType;
import main.java.FSMDE.persistence.InterfaceCollectionXmlManager;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import ERMDE.persistence.tests.TestUtilities;

public class InterfaceCollectionXmlManagerTestCase {

	// private String xmlPath = "interfacetest.xml";

	@Test
	public void testCreateElementOfInterfaceCollection()
			throws ParserConfigurationException {
		InterfaceCollection infacecol = new InterfaceCollection();
		Interface inface = new Interface("interface1", InterfaceType.General,
				InterfaceGraphType.Normal);
		infacecol.add(inface);
		infacecol.add(new Interface("interface2", InterfaceType.General,
				InterfaceGraphType.Normal));
		infacecol.add(new Interface("interface3", InterfaceType.General,
				InterfaceGraphType.Normal));

		Document document = TestUtilities.createDocument();
		Element infaceElementCollection = new InterfaceCollectionXmlManager()
				.getElementFromItem(infacecol, document);

		Element newInface = (Element) infaceElementCollection.getChildNodes()
				.item(0);
		assertEquals(newInface.getAttribute("name"), "interface1");
		newInface = (Element) infaceElementCollection.getChildNodes().item(1);
		assertEquals(newInface.getAttribute("name"), "interface2");
		newInface = (Element) infaceElementCollection.getChildNodes().item(2);
		assertEquals(newInface.getAttribute("name"), "interface3");

	}

	@Test
	public void testCreateInterfaceCollectionFromXml() throws Exception {

		String xml = "<interfaces><interface name='interface1' "
				+ "id='3F2504E0-4F89-11D3-9A0C-0300FFFFFFFF' type='General' graphType='Normal'/>"
				+ "<interface name='inface2' "
				+ "id='3F2504E0-4F89-11D3-9A0C-030DFFFFFFFF' type='General' graphType='Normal'/></interfaces>";

		Document document = TestUtilities.loadXMLFromString(xml);
		Element elem = document.getDocumentElement();
		InterfaceCollection infaceColl = new InterfaceCollectionXmlManager()
				.getItemFromXmlElement(elem);
		assertEquals(
				infaceColl
						.get(UUID
								.fromString("3F2504E0-4F89-11D3-9A0C-030DFFFFFFFF"))
						.getName(), "inface2");
		assertEquals(
				infaceColl
						.get(UUID
								.fromString("3F2504E0-4F89-11D3-9A0C-0300FFFFFFFF"))
						.getType().toString(), "General");
	}

}
